package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    // 查询 某个用户可以看到的所有菜单
    public Result getPermissions(String username) {
        try {
            Integer userId = userDao.getByUsername(username);
            List<Menu> menuList1 = userDao.getMenuLevelIsOne(userId);
            for (Menu menu : menuList1) {
                List<Menu> children = userDao.getByParentMenuIdMenuLevelIsTwo(userId, menu.getId());
                menu.setChildren(children);
            }
            for (Menu menu : menuList1) {
                System.out.println(menu);
            }
            Menu menu = new Menu();
            menu.setName("工作台");
            menu.setIcon("fa-dashboard");
            List<Menu> menuList = new ArrayList<>();
            menuList.add(menu);
            for (Menu menu1 : menuList1) {
                if(!"工作台".equals(menu1.getName())){
                    menuList.add(menu1);
                }

            }
            return new Result(true,MessageConstant.GET_MENU_SUCCESS,menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MENU_FAIL);
        }
    }

    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer start = queryPageBean.getCurrentPage();
        Integer size = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(start, size);
        List<User> userList = new ArrayList<>();
        if (queryString == null) {
            userList = userDao.findPage();
        } else {
            userList = userDao.findPageQueryString(queryString);
        }
        PageInfo pageInfo = new PageInfo<>(userList);
        return new PageResult(pageInfo.getTotal(), userList);
    }

    @Override
    public Result insert(User user,Integer []checkitemIds) {
        try {
            userDao.insert(user);
            System.out.println(user.getId());
            for (Integer roleId : checkitemIds) {
                userDao.insertUserIdAndRoleId(user.getId(),roleId);
            }
            return new Result(true,MessageConstant.ADD_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
        }
    }
    @Override
    public Result getAllRole(){
        try {
            List<Role> roleList = userDao.getAllRole();
            return new Result(true,"获取所有角色信息成功",roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取所有角色信息失败");
        }
    }

    @Override
    public Result findById(Integer userId){
        try {
            User user = userDao.findById(userId);
            if("1".equals(user.getStation())){
                user.setStation("启用");
            }else if ("1".equals(user.getStation())){
                user.setStation("禁用");
            }
            System.out.println(user);
            return new Result(true,"查询编辑信息成功",user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询编辑信息失败");
        }

    }

    @Override
    public Result findRoleIdsByUserId(Integer userId){
        try {
            List<Integer> roleIds = userDao.getByRoleIds(userId);
            return new Result(true,"查询用户角色信息成功",roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询用户角色信息失败");
        }

    }

    @Override
    public Result edit(Integer[] checkitemIds, User user){
        try {
            System.out.println(user.getId());
            userDao.update(user);
            userDao.deleteRoleIdAndUserId(user.getId());
            for (Integer checkitemId : checkitemIds) {
                userDao.insertUserIdAndRoleId(user.getId(),checkitemId);
            }
            return new Result(true,MessageConstant.EDIT_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MEMBER_FAIL);
        }
    }

    @Override
    public Result delete(Integer userId){
        try {
            userDao.deleteRoleIdAndUserId(userId);
            userDao.deleUser(userId);
            return new Result(true,MessageConstant.DELETE_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.DELETE_MEMBER_FAIL);
        }
    }


}
